﻿using System;
using System.Collections.Generic;
using System.Linq;
using ZJFramework10.Extension;

namespace TrainingsApp.IPADApp.Master {
    public partial class ChildrenManager : PageBase {

        public int SchoolId {
            get {
                return ZJFramework10.Utility.QueryStringManager.GetValue<int>("sid");
            }
        }


        protected void Page_Load(object sender, EventArgs e) {
            cbxGrade.DataSource = TrainingsApp.Logic.TS_Grade_Logic.ListAll();
            cbxGrade.ValueField = "Idx";
            cbxGrade.ValueType = typeof(int);
            cbxGrade.TextField = "GradeName";
            cbxGrade.DataBind();
            cbxGrade.Items.Insert(0, new DevExpress.Web.ASPxEditors.ListEditItem("-- 所有年级 --", -1));
            if (!this.IsPostBack) {
                this.cbxGrade.SelectedIndex = 0;
            }

            DataBinds();
        }


        protected void DataBinds() {


            System.Collections.Specialized.StringDictionary queryWheres = new System.Collections.Specialized.StringDictionary();
            if (SchoolId > 0) {

                queryWheres.Add("TS_User_Children.SchoolId", SchoolId.ToString());
            }
            if (this.cbxGrade.SelectedIndex > 0) {
                int cVal = this.cbxGrade.Value.AsInt();

                queryWheres.Add("TS_Class.Grade", cVal.ToString());
            }

            if (!String.IsNullOrEmpty(this.txtEmail.Text)) {
                queryWheres.Add("StuCode", string.Format("%{0}%", this.txtEmail.Text));
            }

            if (!String.IsNullOrEmpty(this.txtIdCard.Text)) {
                queryWheres.Add("IdentityCard", string.Format("%{0}%", this.txtIdCard.Text));
            }

            if (!String.IsNullOrEmpty(this.txtMobile.Text)) {
                queryWheres.Add("Mobile", string.Format("%{0}%", this.txtMobile.Text));
            }
            if (!String.IsNullOrEmpty(this.txtSchoolName.Text)) {
                queryWheres.Add("SchoolName", string.Format("%{0}%", this.txtSchoolName.Text));
            }
            if (!String.IsNullOrEmpty(this.txtUserName.Text)) {
                queryWheres.Add("UserName", string.Format("%{0}%", this.txtUserName.Text));
            }


            var collection = TrainingsApp.Logic.TS_User_Children_Logic.TS_User_Children_ListToGridByPaging(this.aspDataSource.SettingsPager.PageSize, this.aspDataSource.PageIndex + 1, queryWheres);
            this.aspDataSource.DataSource = GetAllPager(aspDataSource.PageIndex, this.aspDataSource.SettingsPager.PageSize, collection.TS_User_Children_List, collection.TotalCount);
            this.aspDataSource.DataBind();

        }

        protected void aspDataSource_PageIndexChanged(object sender, EventArgs e) {
            DataBinds();
        }

        protected void aspDataSource_CustomButtonCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomButtonCallbackEventArgs e) {
            long key = Convert.ToInt64(this.aspDataSource.GetRowValues(e.VisibleIndex, "Idx"));
            if (e.ButtonID == "btnNew") {
                Response.Redirect("ChildrenEdit.aspx" + (SchoolId > 0 ? "?sid=" + SchoolId : string.Empty));
            }
            else if (e.ButtonID == "btnEdit") {
                Response.Redirect("ChildrenEdit.aspx?uid=" + key + (SchoolId > 0 ? "&sid=" + SchoolId : string.Empty));
            }
        }
    }
}